import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

export const changeCounterAsync = createAsyncThunk(
  "counter/change",
  (data, thunkAPI) => {
    console.log(data, "组件调用时传入的参数");
    setTimeout(() => {}, 1000);
    new Promise((resolve, reject) => {
      resolve(88);
    }).then((res) => {
      thunkAPI.dispatch(setCounter(res));
    });
  }
);

const counterSlice = createSlice({
  name: "counter",
  initialState: {
    count: 1,
  },
  reducers: {
    increment: (state, action) => {
      console.log(action);
      state.count += action?.payload || 1;
    },
    setCounter: (state, action) => {
      state.count = action.payload;
    },
  },
});

export const { increment, setCounter } = counterSlice.actions;
export default counterSlice.reducer;
